﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;

namespace SMSPanasonic
{
    public class SQLNotifier : IDisposable
    {
        private event OnChangeEventHandler handler;
        private string tableName;
        public SQLNotifier(string tableName)
        {
            try
            {
                this.tableName = tableName;
                CreatePermission();
                SqlDependency.Start(AppConfig.ConnectionString);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        
        public void Start()
        {
            handler += new OnChangeEventHandler(SQLNotifier_handler);
            this.RegisterDependency();
        }

        private void SQLNotifier_handler(object sender, SqlNotificationEventArgs e)
        {
            RegisterDependency();
        }

        private void RegisterDependency()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(AppConfig.ConnectionString))
                {
                    conn.Open();
                    if (tableName == "pa_MessagePattern")
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT PatternCode, PatternMessage FROM dbo.pa_MessagePattern", conn))
                        {
                            SqlDependency dependency = new SqlDependency(cmd);
                            dependency.OnChange += this.dependency_OnChange;
                            Business.MessagePattern.ReloadData(cmd);
                        }
                    }
                    else if (tableName == "OperatorPrefixCode")
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT prefix_code, operator_code, operator_id, enabled, max_length, min_length FROM dbo.OperatorPrefixCode", conn))
                        {
                            SqlDependency dependency = new SqlDependency(cmd);
                            dependency.OnChange += this.dependency_OnChange;
                            Business.OperatorPrefixCode.ReloadData(cmd);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            ((SqlDependency)sender).OnChange -= this.dependency_OnChange;
            if (handler != null)
                handler(this, e);
        }

        private void CreatePermission()
        {
            // Make sure client has permissions 
            try
            {
                SqlClientPermission perm = new SqlClientPermission(System.Security.Permissions.PermissionState.Unrestricted);
                perm.Demand();
            }
            catch
            {
                throw new ApplicationException("No permission");
            }
        }

        #region IDisposable Members
        public void Dispose()
        {
            SqlDependency.Stop(AppConfig.ConnectionString);
        }
        #endregion
    }
}
